package com.example.module_vip

import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.os.Bundle
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.lib_common.base.BaseActivity
import com.example.module_vip.databinding.ActivityAddVipBinding
import com.example.module_vip.vip.AppointmentsListItem
import com.example.module_vip.vip.viewmodel.VipViewModel
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.util.Calendar

@Route(path = "/vip/AddActivity")
@AndroidEntryPoint
class AddVipActivity : BaseActivity<VipViewModel, ActivityAddVipBinding>() {
    override val mLayoutResId: Int
        get() = R.layout.activity_add_vip
    val viewModel: VipViewModel by viewModels()
    override fun initView() {
        super.initView()

        //   viewModel.createappointment(AppointmentsListItem("2021-09-09 09:09:09", true, listOf(), 1, 1, "123456", "123456", "123456", "123456", true, "123456", "123456"))
    }

    //
    var isselected = false
    override fun initData() {
        super.initData()
        back()

        submitt()

        chanhee()
        timerr()
    }

    private fun timerr() {
        //点击选择日历
        mBinding.laifangshijianVipEdit.setOnClickListener {
            selectrili()

        }
        mBinding.naozhong1.setOnClickListener {
            getkaishishijian()
        }
        mBinding.jieshushijianVipEdit.setOnClickListener {
            getjieshushijian()
        }
    }

    private fun chanhee() {
        mBinding.switchVip.setOnClickListener { it ->
            isselected = !isselected
        }
    }

    private fun submitt() {
        //点击提交
        mBinding.submit.setOnClickListener {
            viewModel.createappointment(
                AppointmentsListItem(   //预约时间
                    "${mBinding.laifangshijianVipEdit.text} - ${mBinding.kaishishijianVipEdit.text} - ${mBinding.jieshushijianVipEdit.text}",
                    true, //
                    listOf(),//	附件内容（多个图片）
                    1, //访问对象（关联员工表 ID）
                    1,//主键id
                    " ${mBinding.shenfenzhenghaoVipEdit.text}",//身份证号
                    "${mBinding.shoujihaomaVipEdit.text}",//手机号码
                    " ${mBinding.cheweishenqingVipEdit.text}",//车牌号码
                    "${mBinding.laifangmudiVipEdit.text}",//来访目的
                    isselected,//是否申请车位
                    "待审核",//	来访状态（待审核、已通过、已完成、驳回）
                    "${mBinding.fangkexingmingVipEdit.text}"//访客姓名
                )
            )
            Toast.makeText(this, "提交成功", Toast.LENGTH_SHORT).show()
            lifecycleScope.launch {
                delay(500)
                finish()
            }

        }

    }

    private fun back() {
        //点击返回
        mBinding.back.setOnClickListener { finish() }
    }

    private fun getjieshushijian() {
        // 获取当前时间
        val calendar = Calendar.getInstance()
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)

        // 创建 TimePickerDialog（24小时制）
        val timePickerDialog = TimePickerDialog(
            this,
            { _, selectedHour, selectedMinute ->
                // 时间选择回调
                val selectedTime = String.format("%02d:%02d", selectedHour, selectedMinute)
                mBinding.jieshushijianVipEdit.setText(selectedTime)
//                    Toast.makeText(this, "选择时间: $selectedTime", Toast.LENGTH_SHORT).show()
            },
            hour, minute, true // 最后一个参数表示是否使用24小时制
        )
        timePickerDialog.show()
    }

    private fun getkaishishijian() {
        // 获取当前时间
        val calendar = Calendar.getInstance()
        val hour = calendar.get(Calendar.HOUR_OF_DAY)
        val minute = calendar.get(Calendar.MINUTE)

        // 创建 TimePickerDialog（24小时制）
        val timePickerDialog = TimePickerDialog(
            this,
            { _, selectedHour, selectedMinute ->
                // 时间选择回调
                val selectedTime = String.format("%02d:%02d", selectedHour, selectedMinute)
                mBinding.kaishishijianVipEdit.setText(selectedTime)
//                    Toast.makeText(this, "选择时间: $selectedTime", Toast.LENGTH_SHORT).show()
            },
            hour, minute, true // 最后一个参数表示是否使用24小时制
        )
        timePickerDialog.show()
    }

    private fun selectrili() {
        //  Toast.makeText(this, "日历", Toast.LENGTH_SHORT).show()
        // 获取当前日期
        val calendar = Calendar.getInstance()
        val year = calendar.get(Calendar.YEAR)
        val month = calendar.get(Calendar.MONTH)
        val day = calendar.get(Calendar.DAY_OF_MONTH)

        // 创建 DatePickerDialog
        val datePickerDialog = DatePickerDialog(
            this,
            { _, selectedYear, selectedMonth, selectedDay ->
                // 用户选择日期后的回调
                val selectedDate = "$selectedYear-${selectedMonth + 1}-$selectedDay"
                mBinding.laifangshijianVipEdit.setText(selectedDate)
//                    Toast.makeText(this, "选择日期: $selectedDate", Toast.LENGTH_SHORT).show()
            },
            year, month, day
        )
        datePickerDialog.show()
    }

    override fun initListener() {
        super.initListener()
    }


}